<?php
	defined( '_JEXEC' ) or die( 'Restricted access' );
	require_once( JApplicationHelper::getPath( 'admin_html' ) );
	JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
	
	switch($task)
	{
		case 'editRoute':
		case 'addRoute':
			editRouteConfig( $option );
			break;
		case 'editPOI':
		case 'addPOI':
			editPOIConfig( $option );
			break;
		case 'editUser':
		case 'addUser':
			editUserConfig( $option );
			break;
		case 'applyRoute':
		case 'saveRoute':
			saveRouteConfig( $option, $task );
			break;
		case 'applyPOI':
		case 'savePOI':
			savePOIConfig( $option, $task );
			break;
		case 'applyUser':
		case 'saveUser':
			saveUserConfig( $option, $task );
			break;
		default:
			showConfig( $option );
			break;
	}
	
	function showConfig( $option )
	{
		$db =& JFactory::getDBO();
		$query = "SELECT * FROM #__sr_routeconfig";
		$db->setQuery( $query );
		$routeRows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		
		$query = "SELECT * FROM #__sr_poiconfig";
		$db->setQuery( $query );
		$poiRows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		
		$query = "SELECT * FROM #__sr_ftablesuserconfig";
		$db->setQuery( $query );
		$userRows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		
		HTML_socialroutes::showConfig( $option, $userRows, $routeRows, $poiRows );
	}
	
	function editRouteConfig($option){
		$row =& JTable::getInstance('RouteConfig', 'Table');
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$id = $cid[0];
		$row->load($id);
		
		$lists = array();
		$lists['active'] = JHTML::_('select.booleanlist', 'active','class="inputbox"', $row->active);

		HTML_socialroutes::editFTConfig($row, $lists, $option);
	}
	
	function editPOIConfig($option){
		$row =& JTable::getInstance('POIConfig', 'Table');
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$id = $cid[0];
		$row->load($id);
		
		$lists = array();
		$lists['active'] = JHTML::_('select.booleanlist', 'active','class="inputbox"', $row->active);

		HTML_socialroutes::editFTConfig($row, $lists, $option);
	}
	
	function editUserConfig($option){
		$row =& JTable::getInstance('FTUserConfig', 'Table');
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$id = $cid[0];
		$row->load($id);
		
		$lists = array();
		$lists['active'] = JHTML::_('select.booleanlist', 'active','class="inputbox"', $row->active);

		HTML_socialroutes::editUserConfig($row, $lists, $option);
	}
	
	function saveRouteConfig($option, $task){
		global $mainframe;
		$row =& JTable::getInstance('RouteConfig', 'Table');
		
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		
		if (!$row->store())
		{
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		switch ($task)
		{
			case 'applyRoute':
				$msg = 'Changes to Configuration saved';
				$link = 'index.php?option=' . $option .'&task=editRoute&cid[]='. $row->id;
				break;
			case 'saveRoute':
			default:
				$msg = 'Configuration Saved';
				$link = 'index.php?option=' . $option;
				break;
		}
		$mainframe->redirect($link, $msg);
	}
	
	function savePOIConfig($option, $task){
		global $mainframe;
		$row =& JTable::getInstance('POIConfig', 'Table');
		
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		
		if (!$row->store())
		{
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		switch ($task)
		{
			case 'applyPOI':
				$msg = 'Changes to Configuration saved';
				$link = 'index.php?option=' . $option .'&task=editPOI&cid[]='. $row->id;
				break;
			case 'savePOI':
			default:
				$msg = 'Configuration Saved';
				$link = 'index.php?option=' . $option;
				break;
		}
		$mainframe->redirect($link, $msg);
	}
	
	function saveUserConfig($option, $task){
		global $mainframe;
		$row =& JTable::getInstance('FTUserConfig', 'Table');
		
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		
		if (!$row->store())
		{
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		switch ($task)
		{
			case 'applyUser':
				$msg = 'Changes to Configuration saved';
				$link = 'index.php?option=' . $option .'&task=editUser&cid[]='. $row->id;
				break;
			case 'saveUser':
			default:
				$msg = 'Configuration Saved';
				$link = 'index.php?option=' . $option;
				break;
		}
		$mainframe->redirect($link, $msg);
	}
?>